/*
 * 接口统一管理
 */
import { request } from '../utils/request'

// 首页相关接口
export const getBannerList = () => {
  return request({
    url: '/banner/list',
    method: 'GET'
  })
}

// 产品相关接口 - 基于后端真实API
export const getProductList = () => {
  return request({
    url: '/product-spu/getListByUser',
    method: 'GET'
  })
}

// 根据商家类型查询产品
export const getProductsByType = (merchantType) => {
  return request({
    url: `/product-spu/getByType?merchantType=${merchantType}`,
    method: 'GET'
  })
}

// 查询一类商家产品
export const getClassFirstProducts = (merchantId) => {
  return request({
    url: `/product-spu/getClassFirst?merchantId=${merchantId}`,
    method: 'GET'
  })
}

// 查询二类商家产品
export const getClassSecondProducts = (merchantId) => {
  return request({
    url: `/product-spu/getClassSecond?merchantId=${merchantId}`,
    method: 'GET'
  })
}

// 查询三类商家产品
export const getClassThirdProducts = (merchantId) => {
  return request({
    url: `/product-spu/getClassThird?merchantId=${merchantId}`,
    method: 'GET'
  })
}

// 根据产品ID获取详情
export const getProductById = (id, merchantType = 1) => {
  return request({
    url: `/product-spu/getProduct?id=${id}&merchantType=${merchantType}`,
    method: 'GET'
  })
}

// 获取产品详情（包含SPU和SKU）
export const getProductDetail = (productId) => {
  return request({
    url: `/product-spu/getProduct?id=${productId}&merchantType=1`,
    method: 'GET'
  })
}

// 获取产品班期信息
export const getProductPeriods = (spuId, merchantId, merchantType = 1) => {
  return request({
    url: `/period-sku/getPeriods?spuId=${spuId}&merchantId=${merchantId}&merchantType=${merchantType}`,
    method: 'GET'
  })
}

// 获取商家信息
export const getMerchantInfo = (spuId) => {
  return request({
    url: `/merchant/getMerchant?spuId=${spuId}`,
    method: 'GET'
  })
}

// 分类相关接口
export const getCategoryList = () => {
  return request({
    url: '/category/list',
    method: 'GET'
  })
}

// 用户相关接口
export const getUserInfo = () => {
  return request({
    url: '/user/info',
    method: 'GET'
  })
}

// 更新用户信息
export const updateUserInfo = (userInfo) => {
  return request({
    url: '/user/updateInfo',
    method: 'POST',
    data: userInfo
  })
}

// 订单相关接口 - 基于后端真实API
export const getOrderList = () => {
  return request({
    url: '/orders/list',
    method: 'GET'
  })
}

// 创建订单
export const createOrder = (orderData) => {
  return request({
    url: '/orders',
    method: 'POST',
    data: orderData
  })
}

// 根据订单ID查询订单详情
export const getOrderById = (orderId) => {
  return request({
    url: `/orders/getOrderById/${orderId}`,
    method: 'GET'
  })
}

// 更新订单状态
export const updateOrderStatus = (orderData) => {
  return request({
    url: '/orders/putOrderByOn',
    method: 'PUT',
    data: orderData
  })
}

// 根据订单号查询订单
export const getOrderByNo = (orderNo) => {
  return request({
    url: `/orders/getOrderByOn/${orderNo}`,
    method: 'GET'
  })
}

// 分页查询订单
export const getOrdersByPage = (pageNum = 1, pageSize = 10, status = null) => {
  let url = `/orders?pageNum=${pageNum}&pageSize=${pageSize}`;
  if (status !== null) {
    url += `&status=${status}`;
  }
  return request({
    url: url,
    method: 'GET'
  })
}

// 支付回调
export const handlePaymentCallback = (callbackData) => {
  return request({
    url: '/orders/payment-callback',
    method: 'POST',
    data: callbackData
  })
}

// 申请退款
export const requestRefund = (orderId, refundData) => {
  return request({
    url: `/orders/${orderId}/refunds`,
    method: 'POST',
    data: refundData
  })
}

// 其他辅助接口
export const getProductInfo = (productId) => {
  return request({
    url: `/merchant/getMerchant?spuId=${productId}`,
    method: 'GET',
  })
}
//申请班期
export const applyPeriod = (callApplyForm) => {
  return request({
    url: `/call-apply/apply`,
    method: 'POST',
    data: callApplyForm
  })

}